# -*- coding: utf-8 -*-
import os
import json
import requests
import time
from multiprocessing import Process, Queue, Pool
import sqlite3


def get_info():
    """ 获取所有图片组的信息 """
    res = []
    with open('info.txt', 'r') as f:
        for line in f:
            data = json.loads(line)
            res.extend(data['data']['list'])
    return res


def get_info_imgs(info):
    """ 获取要下载的所有图片url、目录名、要存储的名字 """
    res = []
    for item in info:
        nickname = item["author"]["nickname"]
        catalog = item["source"]["catalog"]
        name = item["source"]["name"]
        issue = item["issue"]
        pictureCount = item["pictureCount"]
        for pic_idx in range(pictureCount):
            # url = "http://aiss-1254233499.costj.myqcloud.com/picture/%s/%s/%s.jpg" % (catalog, issue, pic_idx)
            url = "http://tuigirl-1254818389.cosbj.myqcloud.com/picture/%s/%s/%s.jpg" % (catalog, issue, pic_idx)
            temp=[name,issue,pic_idx,url,nickname]
            print(temp)
            res.append(temp)
    return res
def download_one(imgs):
    conn = sqlite3.connect('img.sqlite3')
    for img in imgs:
        cursor = conn.cursor()
        try:
            cursor.execute('insert into aiss ("item","title","titleindex","url","user") values ("{}","{}","{}","{}","{}")'.format(img[0],img[1],img[2],img[3],img[4],))
        except:
            pass
        print(img)
    conn.commit()
    cursor.close()




if __name__ == "__main__":
    info = get_info()
    imgs = get_info_imgs(info)
    download_one(imgs)

